<?php
/**
 * 权限方法模型
 * User 1874
 */

namespace app\adminplatform\model;

use think\facade\Cache;
use think\facade\Config;
use think\facade\Request;
use think\Model;

class AuthRule extends Model {
    /*
     * 权限列表
     * @param array $map 查询条件
     */
    public static function authRuleList(array $map=[]){
        return AuthRule::where($map)->field('ar_name,ar_id,ar_method,ar_beforeAfter,ar_status,ar_pid')
            ->paginate(["list_rows"=>PageNum,'query'=>Request::param()]);
    }
    /*
     * 查询单条权限
     * @param array $map 查询条件
     * @param int $type 使用方法类别
     */
    public static function authRuleInfo(array $map=[], int $type=0){
        if($type == 0){
            return AuthRule::where($map)->findOrEmpty();
        }else{
            return AuthRule::where($map)->count("ar_id");
        }
    }
    /*
     * 创建并返回auth_rule权限缓存
     */
    public static function setAuthRule(){
        //获取全部权限
        $auth = AuthRule::field("ar_id,ar_method,ar_beforeAfter,ar_status")->select();
        //空数组
        $array = [];
        foreach ($auth as $value){
            $array[$value["ar_method"]] = $value;
        }
        //放入redis
        Cache::set(Config::get("app.auth_rule"), $array);
        return $array;
    }
    /*
     * 递归权限列表，重组为json
     * @param array $list 权限数组
     * @param int $pid 父id
     * @return array
     */
    public static function findChildAuthRule(array $list, int $pid=0){
        //查询子权限
        /*$map["ar_status"] = 1;
        $map["ar_pid"] = $pid;
        $list = AuthRule::where($map)->field('ar_id,ar_pid,ar_name')->select();

        $array = [];
        foreach ($list as $value) {
            $data['id'] = $value['ar_id'];
            $data['label'] = $value['ar_name'];
            $data['children'] = self::findChildAuthRule($value['ar_id']);
            $array[] = $data;
        }*/
        //$list = 全部权限数据
        $array = [];
        foreach ($list as $key => $value){
            if ($value['ar_pid'] == $pid){
                $data = [];
                $data['id'] = $value['ar_id'];
                $data['label'] = $value['ar_name'];
                $data['children'] = self::findChildAuthRule($list, $value['ar_id']);
                $array[] = $data;
            }
        }
        return $array;
    }
    /*
     * 递归查找该权限所有的上级并正序排列
     * @param int $pid 父id
     */
    public static function getParentAuthRule(int $pid){
        //空数组
        $array = [];
        //查找pid数据
        $list = AuthRule::authRuleInfo(["ar_id"=>$pid])->toArray();
        if($list){
            $array[] = $list;
            $array = array_merge($array, self::getParentAuthRule($list['ar_pid']));
        }
        return $array;
    }
    /**
     * 新增权限数据
     * @param string $method 控制器方法名
     * @param string $name 权限名称
     * @param int $pid 父id
     * @return int
     */
    public static function authRuleAdd(string $method, string $name, int $pid=0){
        return AuthRule::insertGetId(["ar_method"=>$method, "ar_name"=>$name, "ar_pid"=>$pid]);
    }
}